package cn.hlife.com.hlfhaemocyte.systemsetting.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import cn.hlife.com.hlfhaemocyte.MainActivity;
import cn.hlife.com.hlfhaemocyte.R;
import cn.hlife.com.hlfhaemocyte.utils.FragmentUtils;
import cn.hlife.com.hlfhaemocyte.utils.LogUtils;
import cn.hlife.com.hlfhaemocyte.utils.SharedPreferencesUtils;

/**
 * Created by My on 2017/7/9.
 */

public class MaintainSetting extends Fragment {
    private EditText
            etPaidu,
            etBeiLiang,
            etQingXi,
            etShiJi,
            etQingJieYe,
            etQongXueJi,
            et60V, et15V, et9V, et5V, etFuZhu,etXiShiYe;
//            WBC_H_Command,//白细胞阈值 指令1
//            WBC_L_Command,//白细胞阈值 指令2
//            RBC_H_Command,//红细胞阈值 指令1
//            RBC_L_Command,//红细胞阈值 指令2
//            PLT_H_Command,//血小板阈值 指令1
//            PLT_L_Command;//血小板阈值 指令2

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.maintain_setting_layout, container, false);
        ((MainActivity)getActivity()).returnImageView.setVisibility(View.VISIBLE);
        ((MainActivity)getActivity()).returnImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((MainActivity) getActivity()).currentFragment = FragmentUtils.switchFragment
                        (getFragmentManager(), R.id.fragment_content, ((MainActivity) getActivity()).currentFragment, SettingFragment.class, null);
            }
        });


        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        WBC_H_Command = (EditText) view.findViewById(R.id.WBC_H_Command);
//        WBC_L_Command = (EditText) view.findViewById(R.id.WBC_L_Command);
//        RBC_H_Command = (EditText) view.findViewById(R.id.RBC_H_Command);
//        RBC_L_Command = (EditText) view.findViewById(R.id.RBC_L_Command);
//        PLT_H_Command = (EditText) view.findViewById(R.id.PLT_H_Command);
//        PLT_L_Command = (EditText) view.findViewById(R.id.PLT_L_Command);
        etPaidu = (EditText) view.findViewById(R.id.et_paidu);
        etQingXi = (EditText) view.findViewById(R.id.et_qingxi);
        etBeiLiang = (EditText) view.findViewById(R.id.et_beiliang);
        etShiJi = (EditText) view.findViewById(R.id.et_shiji);
        etQingJieYe = (EditText) view.findViewById(R.id.et_qingjieye);
        etQongXueJi = (EditText) view.findViewById(R.id.et_rongxueji);
        et60V = (EditText) view.findViewById(R.id.et_60v);
        et15V = (EditText) view.findViewById(R.id.et_15v);
        et9V = (EditText) view.findViewById(R.id.et_9v);
        et5V = (EditText) view.findViewById(R.id.et_5v);
        etFuZhu = (EditText) view.findViewById(R.id.et_fuzhu);
        etXiShiYe = (EditText) view.findViewById(R.id.et_xishiye);

        initWidgetView();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            saveWidgetData();
        }
        if(!hidden){
            ((MainActivity)getActivity()).returnImageView.setVisibility(View.VISIBLE);

        }
    }

    public void saveWidgetData() {
        JSONObject jsonObject = new JSONObject();
        try {
//            jsonObject.put("WBC_H_Command", WBC_H_Command.getText().toString());
//            jsonObject.put("WBC_L_Command", WBC_L_Command.getText().toString());
//            jsonObject.put("RBC_H_Command", RBC_H_Command.getText().toString());
//            jsonObject.put("RBC_L_Command", RBC_L_Command.getText().toString());
//            jsonObject.put("PLT_H_Command", PLT_H_Command.getText().toString());
//            jsonObject.put("PLT_L_Command", PLT_L_Command.getText().toString());

            jsonObject.put("etPaidu", etPaidu.getText().toString());
            jsonObject.put("etQingXi", etQingXi.getText().toString());
            jsonObject.put("etBeiLiang", etBeiLiang.getText().toString());
            jsonObject.put("etShiJi", etShiJi.getText().toString());
            jsonObject.put("etQingJieYe", etQingJieYe.getText().toString());
            jsonObject.put("etQongXueJi", etQongXueJi.getText().toString());
            jsonObject.put("et60V", et60V.getText().toString());
            jsonObject.put("et15V", et15V.getText().toString());
            jsonObject.put("et9V", et9V.getText().toString());
            jsonObject.put("et5V", et5V.getText().toString());
            jsonObject.put("etFuZhu", etFuZhu.getText().toString());
            jsonObject.put("etXiShiYe", etXiShiYe.getText().toString());
            SharedPreferencesUtils.getInstance().saveMaintainParameter(getContext(), jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initWidgetView() {
        String value = getWidgetData();
        if (TextUtils.isEmpty(value)) {
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject(value);
//            WBC_H_Command.setText(jsonObject.getString("WBC_H_Command"));
//            WBC_L_Command.setText(jsonObject.getString("WBC_L_Command"));
//            RBC_H_Command.setText(jsonObject.getString("RBC_H_Command"));
//            RBC_L_Command.setText(jsonObject.getString("RBC_L_Command"));
//            PLT_H_Command.setText(jsonObject.getString("PLT_H_Command"));
//            PLT_L_Command.setText(jsonObject.getString("PLT_L_Command"));
            etPaidu.setText(jsonObject.getString("etPaidu"));
            etQingXi.setText(jsonObject.getString("etQingXi"));
            etBeiLiang.setText(jsonObject.getString("etBeiLiang"));
            etShiJi.setText(jsonObject.getString("etShiJi"));
            etQingJieYe.setText(jsonObject.getString("etQingJieYe"));
            etQongXueJi.setText(jsonObject.getString("etQongXueJi"));
            et60V.setText(jsonObject.getString("et60V"));
            et15V.setText(jsonObject.getString("et15V"));
            et9V.setText(jsonObject.getString("et9V"));
            et5V.setText(jsonObject.getString("et5V"));
            etFuZhu.setText(jsonObject.getString("etFuZhu"));
            etXiShiYe.setText(jsonObject.getString("etXiShiYe"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private String getWidgetData() {
        return SharedPreferencesUtils.getInstance().getMaintainParameter(getContext());
    }
}
